package cn.iocoder.yudao.module.crm.controller.admin.fielddata;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.fielddata.vo.FieldDataPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.fielddata.vo.FieldDataRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.fielddata.vo.FieldDataSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.fielddata.FieldDataDO;
import cn.iocoder.yudao.module.crm.service.fielddata.FieldDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 数据映射")
@RestController
@RequestMapping("/crm/field-data")
@Validated
public class FieldDataController {

    @Resource
    private FieldDataService fieldDataService;

    @PostMapping("/create")
    @Operation(summary = "创建数据映射")
    @PreAuthorize("@ss.hasPermission('crm:field-data:create')")
    public CommonResult<Long> createFieldData(@Valid @RequestBody FieldDataSaveReqVO createReqVO) {
        return success(fieldDataService.createFieldData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据映射")
    @PreAuthorize("@ss.hasPermission('crm:field-data:update')")
    public CommonResult<Boolean> updateFieldData(@Valid @RequestBody FieldDataSaveReqVO updateReqVO) {
        fieldDataService.updateFieldData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据映射")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:field-data:delete')")
    public CommonResult<Boolean> deleteFieldData(@RequestParam("id") Long id) {
        fieldDataService.deleteFieldData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:field-data:query')")
    public CommonResult<FieldDataRespVO> getFieldData(@RequestParam("id") Long id) {
        FieldDataDO fieldData = fieldDataService.getFieldData(id);
        return success(BeanUtils.toBean(fieldData, FieldDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据映射分页")
    @PreAuthorize("@ss.hasPermission('crm:field-data:query')")
    public CommonResult<PageResult<FieldDataRespVO>> getFieldDataPage(@Valid FieldDataPageReqVO pageReqVO) {
        PageResult<FieldDataDO> pageResult = fieldDataService.getFieldDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FieldDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据映射 Excel")
    @PreAuthorize("@ss.hasPermission('crm:field-data:export')")
    @OperateLog(type = EXPORT)
    public void exportFieldDataExcel(@Valid FieldDataPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FieldDataDO> list = fieldDataService.getFieldDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据映射.xls", "数据", FieldDataRespVO.class,
                BeanUtils.toBean(list, FieldDataRespVO.class));
    }

}